package me.leiusual.sharing.ui.home

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import androidx.paging.PagedList
import me.leiusual.sharing.api.model.storeproductquery.Product
import me.leiusual.sharing.pref.Store
import me.leiusual.sharing.repository.ProductRepository
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-30
 * description:
 */
class HomeSearchProductViewModel @Inject constructor(
    private val productRepository: ProductRepository,
    val store: Store
) : ViewModel() {

  // 商品搜索

  private val _productSearch = MutableLiveData<ParamsProductSearch>()
  val productSearch: LiveData<PagedList<Product>> = switchMap(_productSearch) {
    productRepository.productSearch(it.keyword, it.storeId)
  }

  fun productSearch(keyword: String, storeId: Long) {
    _productSearch.value = ParamsProductSearch(keyword, storeId)
  }

  data class ParamsProductSearch(val keyword: String, val storeId: Long)

}